//
//  CZEmoticonToolBar.swift
//  WeiBo
//
//  Created by Apple on 16/9/8.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

/*
    1. 自定义表情键盘的toolbar
        1. 最近，默认，emoji，浪小花
 */
class CZEmoticonToolBar: UIStackView {

    //  表情toolbar按钮枚举类型
    enum CZEmoticonToolBarButtonType: Int {
        //  最近
        case recent = 1000
        //  默认
        case normal = 1001
        //  emoji
        case emoji = 1002
        //  浪小花
        case lxh = 1003
    }
    
    //  点击toolbar按钮执行的闭包
    var didSelectedButtonClosure: ((CZEmoticonToolBarButtonType)->())?
    
    
    //  上次选中的按钮
    var lastSelectedButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI() {
        //  水平布局
        axis = .horizontal
        //  子控件等比填充
        distribution = .fillEqually
        
        addChildButton(title: "最近", imageName: "compose_emotion_table_left", type: .recent)
        addChildButton(title: "默认", imageName: "compose_emotion_table_mid", type: .normal)
        addChildButton(title: "emoji", imageName: "compose_emotion_table_mid", type: .emoji)
        addChildButton(title: "浪小花", imageName: "compose_emotion_table_right", type: .lxh)
    }
    
    //  添加自按钮
    private func addChildButton(title: String, imageName: String, type: CZEmoticonToolBarButtonType) {
    
        let button = UIButton()
        //  枚举值作为tag值
        button.tag = type.rawValue
        //  点击事件
        button.addTarget(self, action: #selector(btnAction(button:)), for: .touchUpInside)
        
        button.setTitle(title, for: .normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), for: .selected)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  去掉高亮
        button.adjustsImageWhenHighlighted = false
        //  添加控件
        addArrangedSubview(button)
        
        //  默认让其选中第二种(默认表情)
        if type == .normal {
            lastSelectedButton?.isSelected = false
            button.isSelected = true
            lastSelectedButton = button 
        }
        
    }
    
    // MARK:    点击按钮事件
    @objc private func btnAction(button: UIButton) {
        //  取消上次选中
        lastSelectedButton?.isSelected = false
        //  点击的按钮选中
        button.isSelected = true
        //  设置上次选中的按钮
        lastSelectedButton = button
        
        
        //  点击按钮对应的枚举值
        let type = CZEmoticonToolBarButtonType(rawValue: button.tag)!
        //  执行闭包
        didSelectedButtonClosure?(type)
        
    }
    
    //  根据外界传入的section，选中相应的按钮
    func selectedButtonWithSection(section: Int) {
        
        let button = viewWithTag(section + 1000) as! UIButton
        //  上次按钮和这次按钮一样不让去再次选中
        if button == lastSelectedButton {
            return
        }
        
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
        
        
    }
 

}
